﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using control.Biz;

namespace TrangSucShop.administrator.AdminUsercontrol
{
    public partial class user : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["cm"] != null && Request.QueryString["ID"] != null)
                {
                    MT1.ActiveViewIndex = 1;
                    int ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                    BizKhachHang kh = BizKhachHang.GetByID(ID);
                    TextBox1.Text = kh.MaKhachHang;
                    TextBox2.Text = kh.TenKhachHang;
                    TextBox3.Text = kh.DiaChi;
                    TextBox4.Text = kh.Email;
                    TextBox5.Text = kh.SoDienThoai;
                    TextBox6.Text = kh.Username;
                    TextBox7.Text = kh.TaiKhoan;
                    TextBox9.Text = kh.GhiChu;
                    Label2.Text = kh.ID.ToString();
                    List<BizPhanQuyen> quyens = new List<BizPhanQuyen>();
                    quyens = BizPhanQuyen.GetAll();
                    DropDownList1.DataSource = quyens;
                    DropDownList1.DataValueField="ID";
                    DropDownList1.DataTextField = "Quyen";
                    DropDownList1.SelectedValue = kh.Quyen.ID.ToString();
                    DropDownList1.DataBind();
                }
                else
                {
                    MT1.ActiveViewIndex = 0;
                    List<BizKhachHang> khachhangs = new List<BizKhachHang>();
                    khachhangs = BizKhachHang.GetAll();
                    GridView1.DataSource = khachhangs;
                    GridView1.DataBind();
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);
                BizKhachHang kh = new BizKhachHang();
                kh=BizKhachHang.GetByID(ID);
                bool resuil = kh.Detele();
                if (resuil == true)
                    Response.Redirect("user.aspx");
                else
                    Label1.Text = "Không Thể Xóa Dữ Liệu !!!";

            }
            if (e.CommandName == "Edit")
            {
                Response.Redirect("user.aspx?cm=edit&ID=" + e.CommandArgument.ToString());
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");

                l.Attributes.Add("onclick", "javascript:return " +
                     "confirm('xóa record " +
                     DataBinder.Eval(e.Row.DataItem, "ID") + "')");

            }
        }
        #region Khong the xoa
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }
        #endregion
        protected void Button1_Click(object sender, EventArgs e)
        {
            int ID = Convert.ToInt32(Label2.Text);
            BizKhachHang kh = BizKhachHang.GetByID(ID);
            kh.GhiChu = TextBox9.Text;
            BizPhanQuyen q = BizPhanQuyen.GetByID(Convert.ToInt32(DropDownList1.SelectedValue));
            kh.Quyen = q;
            bool resuil = kh.UpDate();
            if (resuil == true)
            {
                Label3.Text = "Sửa Thành Công  ";
                Response.Redirect("user.aspx");
            }
            else
                Label3.Text = "Sửa Thất Bại!!!";
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            List<BizKhachHang> khachhangs = new List<BizKhachHang>();
            khachhangs = BizKhachHang.GetAll();
            GridView1.DataSource = khachhangs;
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }


    }
}